 //验证是否登录
 function checkLogin() {
     //判断是否真正的登录了
     let token = localStorage.getItem('token');
     if (token) {
         //即使有token存在，可能是用户伪造的，需要去验证token是否正确
         $.ajax({
             type: "post",
             url: "my/checkLogin",
             headers: {
                 Authorization: token
             },
             success: function (res) {
                 if (res.status == 200) {
                     $('#header ul').html(`<li>欢迎<span style="color:red">${res.data}</span>登录新闻管理系统</li>
            <li><a href="javascript:;" class="logout">退出</a></li>`)
                 } else if (res.status == 401) {
                     location.href = '/login.html'
                 } else {
                     location.href = '/login.html'
                 }
             }
         });
     } else {
         $('#header ul').html(` <li><a href="javascript:;" class="reg">会员注册</a>/</li>
            <li><a href="javascript:;" class="login">登陆</a></li>`)
     }
 }
 checkLogin();




 layui.use('layer', function () {
     var layer = layui.layer;
     //打开登录的层
     $(document).on('click','.login',function () {
         layer.open({
             type: 2,
             title: '登录',
             content: ['login.html', 'no'],
             area: ['800px', '300px'],
             end: function () {
                 //location.reload();
                 //登录成功后的处理
                 checkLogin();
             }
         });
     });

     //登录验证
     $('.sub').click(function (e) {
         //阻止表单默认行为
         e.preventDefault();
         //接收表单数据
         let data = $('#myform').serialize();
         $.post('api/login', data, function (res) {
             if (res.status == 200) {
                 layer.msg('登录成功', {
                     icon: 6,
                     time: 1000
                 }, function () {
                     //保存token到 localStorage中
                     localStorage.setItem('token', res.data);
                     var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                     parent.layer.close(index); //再执行关闭 
                 })
             } else {
                 layer.msg('登录失败', {
                     icon: 6,
                     time: 3000
                 })
             }
         })
     });

     //退出登录
     $(document).on('click','.logout',function () {
         //清除缓存
         localStorage.removeItem('token');
         $('#header ul').html(`<li><a href="javascript:;" class="reg">会员注册</a>/</li>
            <li><a href="javascript:;" class="login">登陆</a></li>`)
     });


 })